﻿///
/// Copyright (c) 2008, Aaron G. Daisley-Harrison
/// All rights reserved.
/// 
/// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
/// 
/// * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
/// 
/// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
/// 
/// * Neither the name of Daisley-Harison Software nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
/// 
/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
/// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
/// SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
/// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
/// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
/// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
/// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
/// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
/// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;
using System.Windows.Media;
using System.IO;
using System.Windows.Media.Imaging;

namespace DaisleyHarrison.Wavefront
{
    public class DefinedMaterial
    {
        public static readonly scRBGColor DefaultAmbientColor = new scRBGColor(1.0f, 0.2f, 0.2f, 0.2f);
        public static readonly scRBGColor DefaultDiffuseColor = new scRBGColor(1.0f, 0.8f, 0.8f, 0.8f);
        public static readonly scRBGColor DefaultSpecularColor = new scRBGColor(1.0f, 1.0f, 1.0f, 1.0f);
        public enum IlluminationModels
        {
            Flat = 0,
            Ambient = 1,
            Highlight = 2,
            ReflectionRay = 3,
            TransparencyGlassReflectionRay = 4,
            ReflectionFresnelRay = 5,
            TransparencyRefractionRefractionRay = 6,
            TransparencyRefractionRefractionFresnelRay = 7,
            Reflection = 8,
            TransparencyGlass = 9,
            Shadows = 10,
        }
        public string Name { get; set; }
        public scRBGColor Diffuse { get; set; }
        public scRBGColor Ambient { get; set; }
        public scRBGColor Specular { get; set; }
        public string AmbientTexture { get; set; }
        public string DiffuseTexture { get; set; }
        public string SpecularTexture { get; set; }
        public double SpecularExponent { get; set; }
        public double OpticalDensity { get; set; }
        public float Transparency { get; set; }
        public IlluminationModels Illumination { get; set; }
        public DefinedMaterial()
        {
            this.Ambient = DefaultAmbientColor;
            this.Diffuse = DefaultDiffuseColor;
            this.Specular = DefaultSpecularColor;
        }
        public DefinedMaterial(string name)
            :this()
        {
            this.Name = name;
        }
        private MaterialGroup _materialGroup;
        private Brush buildMaterialBrush(string textureFilePath, scRBGColor scRBGColor)
        {
            Brush brush;
            Uri sourceUri;
            if (textureFilePath != null && Uri.TryCreate(textureFilePath, UriKind.Absolute, out sourceUri))
            {
                ImageSource imageSource = new BitmapImage(sourceUri);
                brush = new ImageBrush(imageSource);
            }
            else
            {
                brush = scRBGColor.ToBrush();
            }
            brush.Opacity = this.Transparency;
            return brush;
        }
        public Material BuildMaterial()
        {
            if (_materialGroup == null)
            {
                _materialGroup = new MaterialGroup();
                DiffuseMaterial diffuseMaterial = new DiffuseMaterial();
                diffuseMaterial.Brush = buildMaterialBrush(this.DiffuseTexture, this.Diffuse);
                
                _materialGroup.Children.Add(diffuseMaterial);
                if (this.Illumination == IlluminationModels.Ambient)
                {
                    EmissiveMaterial emissiveMaterial = new EmissiveMaterial();
                    emissiveMaterial.Brush = buildMaterialBrush(this.AmbientTexture, this.Ambient);
                    _materialGroup.Children.Add(emissiveMaterial);
                }
                if (this.Illumination == IlluminationModels.Highlight)
                {
                    SpecularMaterial specularMaterial = new SpecularMaterial();
                    specularMaterial.Brush = buildMaterialBrush(this.SpecularTexture, this.Specular);
                    specularMaterial.SpecularPower = this.SpecularExponent;
                    _materialGroup.Children.Add(specularMaterial);
                }
            }
            return _materialGroup;
        }
        public override string ToString()
        {
            return this.Name;
        }

    }
}
